<template>
  <div class="dashboard">
    <h1>仪表盘</h1>
    
    <el-row :gutter="20" class="stats-cards">
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#409eff"><Message /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ contactStats.total }}</h3>
              <p>联系表单总数</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#f56c6c"><Bell /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ contactStats.unread }}</h3>
              <p>未读消息</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#67c23a"><Check /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ contactStats.replied }}</h3>
              <p>已回复</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#e6a23c"><Menu /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ navbarCount }}</h3>
              <p>导航配置</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="content-section">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最新联系表单</span>
              <el-button type="text" @click="$router.push('/contact')">查看全部</el-button>
            </div>
          </template>
          
          <div v-if="recentContacts.length === 0" class="empty-state">
            <el-empty description="暂无联系表单" />
          </div>
          
          <div v-else>
            <div
              v-for="contact in recentContacts"
              :key="contact.id"
              class="contact-item"
            >
              <div class="contact-info">
                <h4>{{ contact.name }}</h4>
                <p class="contact-subject">{{ contact.subject || '无主题' }}</p>
                <span class="contact-time">{{ formatTime(contact.createdAt) }}</span>
              </div>
              <el-tag
                :type="getStatusType(contact.status)"
                size="small"
              >
                {{ getStatusText(contact.status) }}
              </el-tag>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>快速操作</span>
            </div>
          </template>
          
          <div class="quick-actions">
            <el-button
              type="primary"
              icon="Menu"
              @click="$router.push('/navbar')"
              style="width: 100%; margin-bottom: 10px;"
            >
              管理顶部栏
            </el-button>
            
            <el-button
              type="success"
              icon="HomeFilled"
              @click="$router.push('/homepage')"
              style="width: 100%; margin-bottom: 10px;"
            >
              管理首页内容
            </el-button>
            
            <el-button
              type="warning"
              icon="Message"
              @click="$router.push('/contact')"
              style="width: 100%;"
            >
              查看联系表单
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { request } from '../utils/request'
import { Message, Bell, Check, Menu } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

interface ContactStats {
  total: number
  unread: number
  read: number
  replied: number
  archived: number
}

interface Contact {
  id: number
  name: string
  email: string
  subject?: string
  status: string
  createdAt: string
}

const contactStats = ref<ContactStats>({
  total: 0,
  unread: 0,
  read: 0,
  replied: 0,
  archived: 0
})

const recentContacts = ref<Contact[]>([])
const navbarCount = ref(0)

const getContactStats = async () => {
  try {
    const response = await request.get('/contact/stats/overview')
    if (response.success) {
      contactStats.value = response.data
    }
  } catch (error) {
    console.error('获取联系表单统计失败:', error)
  }
}

const getRecentContacts = async () => {
  try {
    const response = await request.get('/contact/list?page=1&pageSize=5')
    if (response.success) {
      recentContacts.value = response.data.list
    }
  } catch (error) {
    console.error('获取最新联系表单失败:', error)
  }
}

const getNavbarCount = async () => {
  try {
    const response = await request.get('/navbar/config')
    if (response.success) {
      navbarCount.value = response.data.length
    }
  } catch (error) {
    console.error('获取导航配置数量失败:', error)
  }
}

const getStatusType = (status: string) => {
  switch (status) {
    case 'unread': return 'danger'
    case 'read': return 'warning'
    case 'replied': return 'success'
    case 'archived': return 'info'
    default: return 'info'
  }
}

const getStatusText = (status: string) => {
  switch (status) {
    case 'unread': return '未读'
    case 'read': return '已读'
    case 'replied': return '已回复'
    case 'archived': return '已归档'
    default: return '未知'
  }
}

const formatTime = (time: string) => {
  return new Date(time).toLocaleString('zh-CN')
}

onMounted(() => {
  getContactStats()
  getRecentContacts()
  getNavbarCount()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  height: 120px;
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.stat-icon {
  font-size: 40px;
  margin-right: 20px;
}

.stat-info h3 {
  font-size: 28px;
  margin: 0 0 5px 0;
  color: #303133;
}

.stat-info p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.content-section {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.empty-state {
  padding: 20px 0;
}

.contact-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.contact-item:last-child {
  border-bottom: none;
}

.contact-info h4 {
  margin: 0 0 5px 0;
  color: #303133;
  font-size: 14px;
}

.contact-subject {
  margin: 0 0 5px 0;
  color: #606266;
  font-size: 12px;
}

.contact-time {
  color: #909399;
  font-size: 12px;
}

.quick-actions {
  padding: 10px 0;
}
</style>